﻿using System;
using System.Threading.Tasks;
using OpenFileTransfer.Util.Extensions;
using OpenFileTransfer.Util.Zip;
using OpenFileTransfer.Wpf.Services;
using OpenFileTransfer.Wpf.WebSocketServer.Server;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocket.WebSocket.Server;

namespace OpenFileTransfer.Wpf.WebSocketServer.Commands
{
    /// <summary>
    /// 关机命令：教员机发命令关闭所有学员机
    /// </summary>
    [Command(Key = "Shutdown")]
    [AsyncKeyUpperCommandFilter]
    public class ShutdownCommand : IAsyncCommand<WebSocketSession, StringPackageInfo>
    {
        private readonly ILogService _logService;

        public ShutdownCommand(ILogService logService)
        {
            _logService = logService;
        }

        public async ValueTask ExecuteAsync(WebSocketSession session, StringPackageInfo package)
        {
            try
            {
                var data = ZipHelper.GZipDecompressString(package.Body);
                //var item = JsonHelper.DeserializeObject<ShutdownItem>(data);
                //await _webSocketServerService.ShutdownClientsAsync(item);
                await Task.CompletedTask;
            }
            catch (Exception e)
            {
                _logService.Error($"解析参数失败：{e.GetFullMessage()}, body = {package.Body}.");
            }
        }
    }
}
